﻿// <copyright file="MoveCounter.xaml.cs" company="ALCPU">
// Copyright (c) 2010 All Right Reserved
// </copyright>
// <author>Arthur Liberman</author>
// <email>Arthur_Liberman@hotmail.com</email>
// <date>04-06-2010</date>
// <summary>The move counter codebehind.</summary>

namespace Chess
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;

    /// <summary>
    /// Interaction logic for MoveCounter.xaml
    /// </summary>
    public partial class MoveCounter : UserControl
    {
        /// <summary>
        /// The scroll up Storyboard.
        /// </summary>
        private readonly Storyboard scrollUpStoryboard;

        /// <summary>
        /// The scroll down Storyboard.
        /// </summary>
        private readonly Storyboard scrollDowStoryboard;
        
        /// <summary>
        /// The counter's value.
        /// </summary>
        private int counter;

        /// <summary>
        /// Initializes a new instance of the <see cref="MoveCounter"/> class.
        /// </summary>
        public MoveCounter()
        {
            this.InitializeComponent();

            this.text1.Text = "0";
            this.text2.Text = "0";
            this.counter = 0;

            this.scrollDowStoryboard = this.FindResource("ScrollDown") as Storyboard;
            this.scrollUpStoryboard = this.FindResource("ScrollUp") as Storyboard;

            this.scrollUpStoryboard.Completed += new EventHandler(this.SingleScroll_Completed);
            this.scrollDowStoryboard.Completed += new EventHandler(this.SingleScroll_Completed);
        }

        /// <summary>
        /// Gets or sets the value.
        /// </summary>
        /// <value>The value.</value>
        public int Value 
        {
            get
            {
                return this.counter;
            }

            protected set 
            {
                this.AnimateCount(value);
                this.counter = value; 
            }
        }

        #region Operator Overloading

        /// <summary>
        /// Implements the operator ++.
        /// </summary>
        /// <param name="moveCounter">The move counter.</param>
        /// <returns>The result of the operator.</returns>
        public static MoveCounter operator ++(MoveCounter moveCounter)
        {
            moveCounter.Value++;
            return moveCounter;
        }

        /// <summary>
        /// Implements the operator --.
        /// </summary>
        /// <param name="moveCounter">The move counter.</param>
        /// <returns>The result of the operator.</returns>
        public static MoveCounter operator --(MoveCounter moveCounter)
        {
            moveCounter.Value--;
            return moveCounter;
        }

        /////// <summary>
        /////// Implements the operator ==.
        /////// </summary>
        /////// <param name="leftCounter">The left counter.</param>
        /////// <param name="rightCounter">The right counter.</param>
        /////// <returns>The result of the operator.</returns>
        ////public static bool operator ==(MoveCounter leftCounter, MoveCounter rightCounter)
        ////{
        ////    return leftCounter.counter == rightCounter.counter;
        ////}

        /////// <summary>
        /////// Implements the operator !=.
        /////// </summary>
        /////// <param name="leftCounter">The left counter.</param>
        /////// <param name="rightCounter">The right counter.</param>
        /////// <returns>The result of the operator.</returns>
        ////public static bool operator !=(MoveCounter leftCounter, MoveCounter rightCounter)
        ////{
        ////    return leftCounter.counter != rightCounter.counter;
        ////}

        /////// <summary>
        /////// Implements the operator &gt;.
        /////// </summary>
        /////// <param name="leftCounter">The left counter.</param>
        /////// <param name="rightCounter">The right counter.</param>
        /////// <returns>The result of the operator.</returns>
        ////public static bool operator >(MoveCounter leftCounter, MoveCounter rightCounter)
        ////{
        ////    return leftCounter.counter > rightCounter.counter;
        ////}

        /////// <summary>
        /////// Implements the operator &lt;.
        /////// </summary>
        /////// <param name="leftCounter">The left counter.</param>
        /////// <param name="rightCounter">The right counter.</param>
        /////// <returns>The result of the operator.</returns>
        ////public static bool operator <(MoveCounter leftCounter, MoveCounter rightCounter)
        ////{
        ////    return leftCounter.counter < rightCounter.counter;
        ////}

        /////// <summary>
        /////// Implements the operator &gt;=.
        /////// </summary>
        /////// <param name="leftCounter">The left counter.</param>
        /////// <param name="rightCounter">The right counter.</param>
        /////// <returns>The result of the operator.</returns>
        ////public static bool operator >=(MoveCounter leftCounter, MoveCounter rightCounter)
        ////{
        ////    return leftCounter.counter >= rightCounter.counter;
        ////}

        /////// <summary>
        /////// Implements the operator &lt;=.
        /////// </summary>
        /////// <param name="leftCounter">The left counter.</param>
        /////// <param name="rightCounter">The right counter.</param>
        /////// <returns>The result of the operator.</returns>
        ////public static bool operator <=(MoveCounter leftCounter, MoveCounter rightCounter)
        ////{
        ////    return leftCounter.counter <= rightCounter.counter;
        ////}

        #endregion

        /// <summary>
        /// Resets the counter.
        /// </summary>
        public void ResetCounter()
        {
            this.Value = 0;
        }

        /// <summary>
        /// Handles the Completed event of the SingleScroll control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void SingleScroll_Completed(object sender, EventArgs e)
        {
            this.text1.Text = this.text2.Text;
            this.text1.Foreground = this.text2.Foreground;
            this.text1.RenderTransform = this.text2.RenderTransform.Clone();
        }

        /// <summary>
        /// Animates the count.
        /// </summary>
        /// <param name="value">The value.</param>
        private void AnimateCount(int value)
        {
            this.text2.Text = value.ToString();
            if (value >= 0 && this.text2.Foreground != Brushes.Black)
            {
                this.text2.Foreground = Brushes.Black;
            }
            else if (value < 0 && this.text2.Foreground != Brushes.Red)
            {
                this.text2.Foreground = Brushes.Red;
            }

            if (value > this.counter)
            {
                // Scroll Up
                this.scrollUpStoryboard.Begin();
            }
            else if (value < this.counter)
            {
                // Scroll Down
                this.scrollDowStoryboard.Begin();
            }
        }
    }
}